//
//  HomePageBeforeListVC.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/4/20.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "HomePageBeforeListVC.h"
#import "HomePageDataModel.h"
@interface HomePageBeforeListVC ()<UICollectionViewDelegate, UICollectionViewDataSource, QCWaterfallsFlowLayoutDelegate>

@property (nonatomic, retain) YHYBaseCollectionView *beforeListCollectionView;

@property (nonatomic, retain) QCWaterfallsFlowLayout *waterFlowLayout;

@property (nonatomic, retain) NSMutableArray *dataSource;

@end

@implementation HomePageBeforeListVC

- (void)dealloc {
    
    _beforeListCollectionView.delegate = nil;
    _beforeListCollectionView.dataSource = nil;
    
    [_beforeListCollectionView release];
    [_dataSource release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = self.month;
    titleLabel.font = [UIFont boldSystemFontOfSize:22];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
    
    //导航栏左侧按钮
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"< 往期列表" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemAction:)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    self.dataSource = [NSMutableArray array];
    
    [self creatView];
    
    [self dataHandle];

    
}

- (void)leftItemAction:(UIBarButtonItem *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark - 创建视图
- (void)creatView {
    
    self.waterFlowLayout = [[QCWaterfallsFlowLayout alloc] init];
    _waterFlowLayout.numberOfColumns = 2;
    _waterFlowLayout.delegate = self;
    
    
    self.beforeListCollectionView = [[YHYBaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 114) collectionViewLayout:_waterFlowLayout];
    [_beforeListCollectionView registerClass: [HomePageBeforeListDetailCVC class]forCellWithReuseIdentifier:@"List"];
    _beforeListCollectionView.backgroundColor = [UIColor lightGrayColor];
    _beforeListCollectionView.delegate = self;
    _beforeListCollectionView.dataSource = self;
    [self.view addSubview:_beforeListCollectionView];
    
}

#pragma mark - 协议方法实现

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HomePageBeforeListDetailCVC *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"List" forIndexPath:indexPath];
    
    cell.dataModel = self.dataSource[indexPath.item];
    
    return cell;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView heightForPhotoAtIndexPath:(NSIndexPath *)indexPath withWidth:(CGFloat)width {
    return 130;
}



- (CGFloat)collectionView:(UICollectionView *)collectionView heightForAnnotationAtIndexPath:(NSIndexPath *)indexPath withWidth:(CGFloat)width {
    
    // 文字边距预留
    CGFloat annotationPadding = 4.0;
    
    // 文字头高度
    CGFloat annotationHeaderHeight = 17.0;
    
    HomePageDataModel *model = self.dataSource[indexPath.item];
    
    UIFont *font = [UIFont systemFontOfSize:12];
    
    // 根据文字大小和宽度 计算文字高度
    CGFloat commentHeight = [model heightForDetail:font width:width];
    
    // 总高度
    CGFloat height = annotationPadding + commentHeight + annotationHeaderHeight + annotationPadding;
    
    return height;
    
}

/**
 *  点击跳转下一页
 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HomePageBeforeListDetailVC *detailVC = [[HomePageBeforeListDetailVC alloc] init];
    
    HomePageDataModel *model = self.dataSource[indexPath.item];
    
    detailVC.dataModel = model;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

#pragma mark - 数据解析
- (void)dataHandle {
    
    NSString *string = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/hp/bymonth/%@", self.month];
    
    [YHYAFNetTool GETNetWirhUrl:string body:nil headerFile:nil response:YHYJSON Success:^(id result) {
        
        NSLog(@"%@", result);
        
        for (NSDictionary *dic in result[@"data"]) {
            HomePageDataModel *model = [[HomePageDataModel alloc] initWithDataSource:dic];
            
            [self.dataSource addObject:model];
            
        }
        
        [self.beforeListCollectionView reloadData];
        
    } Failure:^(NSError *error) {
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
